package com.example.administrator.timertask;

import android.animation.ValueAnimator;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.animation.LinearInterpolator;
import android.widget.TextView;

import com.example.administrator.timertask.util.TimeUtil;

import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.TimeUnit;

import io.reactivex.Flowable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Action;
import io.reactivex.functions.Consumer;

/**
 * @author: njb
 * @date: 2020/3/7 0007 12:57
 * @desc:
 */
public class CountDownActivity extends AppCompatActivity {
    private TextView textView;
    private int count = 20;
    private long second = 1800;
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == 1) {
                if (second > 0) {
                    textView.setText(TimeUtil.formatTime(Integer.parseInt(String.valueOf(second))));
                    second--;
                    handler.sendEmptyMessageDelayed(1, 1000);
                } else {
                    textView.setText(TimeUtil.getFormatHMS(second));
                }
            }
           /* int count = msg.arg1;
            if (count == 0) {
                textView.setText("" + count);
            } else {
                textView.setText(count + "");
            }*/
        }
    };
    private View mStart;
    private Disposable mdDisposable;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_countdown);
        initView();
        //1.使用CountDownTimer实现倒计时
        // initCount();
        //2.使用动画实现倒计时
        //initAnimator();
        //3.使用Timer实现倒计时
        //initTimer();
        //4.使用Rxjava实现倒计时
      //  initRxJava();
        //5.使用handler实现倒计时
        initHandler();
    }

    private void initHandler() {
        handler.sendEmptyMessageDelayed(1,1000);
    }


    private void initView() {
        textView = findViewById(R.id.tv_time);
  /*      textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                initRxJava();
            }
        });*/
    }

    private void initCount() {
        /**
         * CountDownTimer timer = new CountDownTimer(3000, 1000)中，
         * 第一个参数表示总时间，第二个参数表示间隔时间。
         * 意思就是每隔一秒会回调一次方法onTick，然后1秒之后会回调onFinish方法。
         */
        CountDownTimer timer = new CountDownTimer(10000, 1000) {
            public void onTick(long millisUntilFinished) {
                textView.setText("倒计时" + millisUntilFinished / 1000 + "秒");
            }

            public void onFinish() {
                textView.setText("倒计时结束");
            }
        };
        //调用 CountDownTimer 对象的 start() 方法开始倒计时，也不涉及到线程处理
        timer.start();
    }


    private void initAnimator() {
        ValueAnimator animator = ValueAnimator.ofInt(5, 0);
        //设置时间
        animator.setDuration(20000);
        //均匀显示
        animator.setInterpolator(new LinearInterpolator());
        //监听
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {

            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                int value = (Integer) animation.getAnimatedValue();
                textView.setText(value + "");
                if (value == 0) {
                    textView.setText(value + "");
                }
            }
        });
        animator.start();
    }

    private void initTimer() {
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {

            @Override
            public void run() {
                Message message = new Message();
                message.arg1 = count;
                if (count != -1) {
                    count--;
                } else {
                    return;
                }
                handler.sendMessage(message);
            }
        }, 1000, 1000);
    }

    /**
     * RxJava 方式实现
     */
    private void initRxJava() {
        mdDisposable = Flowable.intervalRange(0, 11, 0, 1, TimeUnit.SECONDS)
                .observeOn(AndroidSchedulers.mainThread())
                .doOnNext(new Consumer<Long>() {
                    @Override
                    public void accept(Long aLong) throws Exception {
                        textView.setText("重新获取(" + (10 - aLong) + ")");
                    }
                })
                .doOnComplete(new Action() {
                    @Override
                    public void run() throws Exception {
                        //倒计时完毕置为可点击状态
                        textView.setEnabled(true);
                        textView.setText("获取验证码");
                    }
                })
                .subscribe();

    }
}
